package persentation.presentUniversityDean;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import vo.userVO.StudentVO;
import businesslogic.courseBl.CourseController;
import businesslogic.userBl.UniversityDeanController;

public class StudentInfoPanel extends JPanel {

	private MyTabbedPane tabbedPane;
	private JTable table;
	private ArrayList<StudentVO> studentVOs;

	private UniversityDeanController uDeanController;
	private CourseController courseController;

	private String[] departmentList = new String[29];
	private String[] gradeList = new String[6];

	/**
	 * Create the panel.
	 */
	public StudentInfoPanel(UniversityDeanController uDeanController) {
		setLayout(null);
		this.uDeanController = uDeanController;
		try {
			studentVOs = uDeanController.getStudentList();
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			ExceptionHandler.networkException();
		}
		courseController = new CourseController(uDeanController.getUserID());
		ArrayList<String> departmentArrayList = null;
		try {
			departmentArrayList = courseController
					.getAllDepartments();
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
			ExceptionHandler.networkException();
			
		}
		departmentList[0] = "全部";
		for (int i = 0; i < departmentArrayList.size(); i++) {
			departmentList[i + 1] = departmentArrayList.get(i);
		}

		gradeList[0] = "全部";
		gradeList[1] = "大一";
		gradeList[2] = "大二";
		gradeList[3] = "大三";
		gradeList[4] = "大四";
		gradeList[5] = "大五";

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(106, 83, 565, 347);
		add(scrollPane);

		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
			},
			new String[] {
				"\u5B66\u53F7", "\u59D3\u540D", "\u5E74\u7EA7", "\u6240\u5C5E\u9662\u7CFB", "\u6BD5\u4E1A\u8D44\u683C"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class, String.class, String.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(3).setResizable(false);
		table.getColumnModel().getColumn(4).setResizable(false);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		scrollPane.setViewportView(table);

		final JComboBox comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					changeStudentList(departmentList[comboBox
							.getSelectedIndex()]);
				}
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(departmentList));
		comboBox.setBounds(175, 24, 165, 33);
		add(comboBox);

		/*final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					changeStudentList(departmentList[comboBox_1
							.getSelectedIndex()]);
				}
			}
		});
		comboBox_1.setMaximumRowCount(5);
		comboBox_1.setModel(new DefaultComboBoxModel(gradeList));
		comboBox_1.setBounds(514, 24, 113, 33);
		add(comboBox_1);
*/
		JLabel label = new JLabel("院系");
		label.setBounds(110, 24, 36, 33);
		add(label);

		/*JLabel label_1 = new JLabel("年级");
		label_1.setBounds(445, 24, 36, 33);
		add(label_1);*/
		fillTable();
		/*JButton button = new JButton("查看详细信息");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StudentVO studentVO = studentVOs.get(table.getSelectedRow());
				System.out.println(table.getSelectedRow());
				System.out.println(studentVO.getName());

				tabbedPane.addOne(studentVO.getId() + " 详细信息", new InfoPanel(
						studentVO));

			}
		});
		button.setBounds(312, 441, 111, 25);
		add(button);
*/
	}

	public void fillTable() {
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		tableModel.setRowCount(0);
		for (StudentVO student : studentVOs) {
			String[] array = new String[8];
			array[0] = student.getId();
			array[1] = student.getName();
			array[2] = String.valueOf(student.getGrade());
			array[3] = student.getDepartmentId();
			array[4] = "无相关信息";
			array[5] = "无相关信息";
			array[6] = "无相关信息";
			array[7] = "无相关信息";

			System.out.println(array[0]);
			System.out.println(array[1]);
			System.out.println(array[2]);
			System.out.println(array[3]);
			System.out.println(array[4]);
			System.out.println(array[5]);
			System.out.println(array[6]);
			System.out.println(array[7]);
			tableModel.addRow(array);
			table.invalidate();
			table.updateUI();
		}
	}

	public void changeStudentList(String departmentID) {
		if (departmentID.equals("全部")) {
			try {
				studentVOs = uDeanController.getStudentList();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
				}
		} else {
			try {
				studentVOs = uDeanController.getStudentList(departmentID);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		fillTable();
	}

	public void setMyTabbedPane(MyTabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}
}
